// JavaScript Document
$(function(){
	$( document ).on( "click", ".userlead", function( e ){
		$(".userguide").slideDown("500");
		$.ajax({
			   
				url: "/User/Index/finshover",
				data: "1=1",
				type:"post",
				dataType: "json",
				success:function(ret){
					
				}
			
		});
	});
	$( document ).on( "click", ".userlead2", function( e ){
		$(".userguide").slideToggle("500");
		$.ajax({
			   
				url: "/User/Index/finsh",
				data: "1=1",
				type:"post",
				dataType: "json",
				success:function(ret){
					
				}
			
		});
	});
	$( document ).on( "mouseover", ".en", function( e ){
		$(this).addClass('huaguo');
	});
	$( document ).on( "mouseout", ".en", function( e ){
		$(this).removeClass('huaguo');
	});
	$( document ).on( "click", ".en", function( e ){
		$(".en").not(this).removeClass('huaguodz');
		$(this).addClass('huaguodz');
	});
	$( document ).on( "click", "#gopre", function( e ){
		var got = $(this).attr('go');
		if(got==0) return false;
		var num = parseInt(got)+1;
		var step = "step"+got;
		$("#step"+got).show();
		$("#step"+num).hide();
		$(".step").removeClass("step"+num);
		$(".step").addClass("step"+got);
		
		$(".to"+num).removeClass("num"+num);
		$(".to"+num).addClass("n"+num);
		
		$(".to"+got).removeClass("n"+got);
		$(".to"+got).addClass("num"+got);
	});
	$( document ).on( "click", "#gonext", function( e ){
		var got = $(this).attr('go');
		if(got==0) return false;
		if(got==6)
		{
			$(".userguide").slideToggle("500");
			$.ajax({
			   
				url: "/User/Index/finsh",
				data: "1=1",
				type:"post",
				dataType: "json",
				success:function(ret){
					
				}
			
			});
			return false;
		}
		var num = parseInt(got)-1;
		var step = "step"+got;
		
		if(num==1)
		{
			var first_name = trim($("#first_name").val());
			var last_name = trim($("#last_name").val());
			if(first_name=='FIRST NAME (名)') first_name='';
			if(last_name=='LAST NAME (姓)') last_name='';
			if(!isTrueName(first_name)&&first_name!='') return alert('FIRST_NAME只能用英文并且控制在30个字符以内！');
			if(!isTrueName(last_name)&&last_name!='') return alert('LAST_NAME只能用英文并且控制在30个字符以内！');
			$.ajax({
			   
				url: "/User/Index/stepOne",
				data: "first_name="+first_name+"&last_name="+last_name,
				type:"post",
				dataType: "json",
				success:function(ret){
					if(ret.status==0)  return alert($ret.info);
				}
			
			});
			
		}
		if(num==2)
		{
			var hangye = trim($("#hangye").val());
			var zhineng = trim($("#zhineng").val());
			if(zhineng=='请输入您期望的职位') zhineng='';
			$.ajax({
			   
				url: "/User/Index/stepTwo",
				data: "hangye="+hangye+"&zhineng="+zhineng,
				type:"post",
				dataType: "json",
				success:function(ret){
					if(ret.status==0)  return alert($ret.info);
				}
			
			});
			
		}
		$("#step"+got).show();
		$("#step"+num).hide();
		$(".step").removeClass("step"+num);
		$(".step").addClass("step"+got);
		$(".to"+num).removeClass("num"+num);
		$(".to"+num).addClass("n"+num);
		$(".to"+got).removeClass("n"+got);
		$(".to"+got).addClass("num"+got);
	});
	$( document ).on( "click", ".cancelclass", function( e ){
		var tid = $(this).attr('tid');
		if(!tid||tid==0) return alert('非法请求！');
		var sj = $(this).attr('sj');
		if(confirm("您确定要取消 "+sj+"的课程吗？")){
			$(this).removeClass('cancelclass');
			$.ajax({
				   
				url: "/User/Index/cancelClass",
				data: "new_classroom_id="+tid,
				type:"post",
				dataType: "json",
				success:function(ret){
					alert(ret.info);
					if(ret.status==1)  location.reload() ;
					if(ret.status==0)  $(".qxyy").addClass('cancelclass');;
				}
			
			});
		}
	});
	$( document ).on( "click", ".my_order_a1", function( e ){
//	$(".my_order_a1").live('click',function(){
		var sid = $(this).attr("sid");
                var tid = $(this).attr("tid");
			
                if(tid == 0){
                    $("#tip_my_buy").find(".tip_c").find("p").html("您确定要删除该订单吗？删除订单后，不能恢复了哦。");
                }else{
                    $("#tip_my_buy").find(".tip_c").find("p").html("您确定要删除该订单吗？与该单一起拆分的所有订单都会被删除。");         
                }
                     
		$("#tip_my_buy").show();
		$("#tip_my_buy_summbit").attr("sid",sid);
	});

	$( document ).on( "click", "#tip_my_buy_close", function( e ){
//	$("#tip_my_buy_close").live('click',function(){
		$("#tip_my_buy").hide();
	});
	$(document).on("click", ".hTop_r_exit", function(e) {
        //	$(".hTop_r_exit").live('click',function(){
        $.ajax({
            url: "/User/Login/loginExit",
            data: "mod=userlogin&act=ajaxexit",
            type: "post",
            dataType: "json",
            success: function(ret) {
                window.location.reload();
            }
        });
    });
	$( document ).on( "click", "#tip_my_buy_summbit", function( e ){
//	$("#tip_my_buy_summbit").live('click',function(){
		var sid = $(this).attr("sid");
                var tokenid = $(this).attr("tokenid");
		$.ajax({
			url: "/User/Order/delOrder.html",
			data: "sid="+encodeURIComponent(sid),
			type:"post",
			dataType: "json",
			success:function(ret){
				
				if(ret.status == 0){
					alert(ret.info);
                    $("#tip_my_buy").hide();
				}else if(ret.status == 1){
                    window.location.reload();
//					$("#buyRrecords_"+sid).remove();
					$("#tip_my_buy").hide();
				}else if(ret.status == -100){
					window.location.reload();
				}
			}
		});
	});
	$( document ).on( "click", ".my_score_button_summit", function( e ){
		//	$(".my_score_button_summit").live('click',function(){
		
		var interest = $("input:radio[name='interest']:checked").val();
		var interaction = $("input:radio[name='interaction']:checked").val();
		var pronun = $("input:radio[name='pronun']:checked").val();
		var explain = $("input:radio[name='explain']:checked").val();
		var rhythm = $("input:radio[name='rhythm']:checked").val();
		var easy = $("input:radio[name='easy']:checked").val();
		var matching = $("input:radio[name='matching']:checked").val();
		var matching_message = $("#matching_message").val();
		var cid = $("#cid").val();
		var type = $("#type").val();
		var gradeid = $("#gradeid").val();
		var fluency = $("input:radio[name='fluency']:checked").val();
		var obj  = $(this);
		var my_score_intro_obj = obj.parents(".my_score_intro");
		var my_score_c_obj = obj.parents(".my_score_c").find(".my_comment_res");
		var my_score_button_down_obj = obj.parents(".my_score_c").find(".my_score_button_down");
		var reg__tip_code_obj = obj.parents(".my_score_c").find("span");
		
		var my_comment = $(".my_comment").val();
		if(cid=="")
		{
			reg__tip_code_obj.show();
			reg__tip_code_obj.html("课程信息错误！");
		}
		
		if(my_comment == "" || my_comment =="对你的外教说几句吧"){
			
			//			reg__tip_code_obj.css("visibility","inherit");
			reg__tip_code_obj.show();
			reg__tip_code_obj.html("评价不能为空");
			return;
		} 
		
		if(!interest || !interaction || !pronun || !explain || !rhythm || !easy ||!matching ||!fluency){
			reg__tip_code_obj.show();
			reg__tip_code_obj.html("请选择完整");
		}else{
			reg__tip_code_obj.show();
			reg__tip_code_obj.html("");
			$.ajax({
				url: "/User/Grade/postGrade.html",
				data: "interest="+encodeURIComponent(interest)+"&interaction="+encodeURIComponent(interaction)+"&pronun="+encodeURIComponent(pronun)+"&explain="+encodeURIComponent(explain)+"&rhythm="+encodeURIComponent(rhythm)+"&easy="+encodeURIComponent(easy)+"&matching="+encodeURIComponent(matching)+"&matching_message="+encodeURIComponent(matching_message)+"&fluency="+encodeURIComponent(fluency)+"&my_comment="+encodeURIComponent(my_comment)+"&cid="+encodeURIComponent(cid)+"&type="+encodeURIComponent(type)+"&id="+encodeURIComponent(gradeid),
				type:"post",
				dataType: "json",
				success:function(ret){
					if(ret.status == 1){
						alert('保存成功！');
						location.href='/User/Grade.html';
					}else{
						//						reg__tip_code_obj.css("visibility","inherit");
						reg__tip_code_obj.show();
						reg__tip_code_obj.html(ret.text);
					}
				}
			});
		}
	});

	
});

function trim(s)
{  
	var reg='/&nbsp;/g';
	var i = s.replace(reg,'');
	return  i.replace(/(^\s*)|(\s*$)/g,'');
}  
//校验用户名：只能输入1-30个字母
function isTrueName(s)  
{  
	var patrn=/^[a-zA-Z]{1,30}$/;  
	if (!patrn.exec(s)) return false  
	return true  
} 
//校验密码：只能输入6-20个字母、数字、下划线  
function isPasswd(s)  
{  
	var patrn=/^(\w){6,20}$/;  
	if (!patrn.exec(s)) return false  
	return true  
}  
//校验普通电话、传真号码：可以“+”开头，除数字外，可含有“-”  
function isTel(s)  
{  
	//var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?(\d){1,12})+$/;  
	var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;  
	if (!patrn.exec(s)) return false  
	return true  
}  
//校验手机号码：必须以数字开头，除数字外，可含有“-”  
function isMobil(s)  
{  
	var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;  
	if (!patrn.exec(s)) return false  
	return true  
}  
//校验邮政编码  
function isPostalCode(s)  
{  
	//var patrn=/^[a-zA-Z0-9]{3,12}$/;  
	var patrn=/^[a-zA-Z0-9 ]{3,12}$/;  
	if (!patrn.exec(s)) return false  
	return true  
}  


//倒计时程序
function Timing(time,obj,msg){
	var t=eval(parseInt(time)+1);//+1,初始页面显示time，而非(time-1);
	$("#"+obj).attr('djs',t);
	function Countdown()
	{
		if(t>0)
		{
			t--;
			$("#"+obj).attr('djs',t);
			var action=setTimeout(Countdown,1000);
		}
		else
		{
			clearTimeout(action);
			var tid = $("#"+obj).attr('tid');
			$("#"+obj).wrap('<a href="'+HAOWJ+'/play.htm?rank=packClass&id='+tid+'" target="_blank"></a>');
			$("#"+obj).removeClass('nojrjs');
			$("#"+obj).addClass('jrjs');
		}
	};
	Countdown();
};

//以下为发送消息文本框字数检测
var text_limit = 140;
function counterLen(txt)
{
	var e_len = 0, c_len = 0, len, i;
	txt_arr = txt.replace(/(\r\n)$/gm,'').split('');
	len = txt_arr.length;

	for(i = 0; i < len; i++){
		if(txt_arr[i].charCodeAt(0) > 255){
			c_len++;
		}else{
			e_len++;
		}
	}

	return (Math.round(e_len / 2) + c_len);
}

function textareaCheck(obj){
	var flag = false;
	var value = obj.val();
	var length = counterLen(value);
	//字数限制为140
	if(length > text_limit){
		//输入框闪烁
		/*obj.animate({
		backgroundColor:'#FFCCCC'
		}, 20);*/
		setTimeout(function(){
			obj.get(0).style.backgroundColor = "#FFCCCC";
			setTimeout(function(){
				obj.get(0).style.backgroundColor = "#FFFFFF";
			},500);
		},500);
		flag = true;
	}
	return flag;
}
/*
 * 获得焦点与失去焦点
 */
function dis(con)
{
	if(con==1)
	{
		$("#matching_message").attr("disabled",false);	
	}
	else
	{
		$("#matching_message").attr("disabled",true);
		$("#matching_message").val('');
	}
}
